﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        HtmlControl MenuSearch = (HtmlControl)Master.FindControl("MenuSearch");
        MenuSearch.Attributes.Add("class", "active");

        HtmlControl aSearch = (HtmlControl)Master.FindControl("aSearch");
        aSearch.Attributes.Add("class", "active");


        //string dbandUser = CodeConfig.DatabaseAndUser;

        string strPolygonDB = CodeConfig.PolygonDB;
        string strPointDB = CodeConfig.PointDB;

        string strMarine = "Active"; //Todo: move this back when we get wdpa-marine up and going -Convert.ToString(Session["Marine"]);

        if (strMarine == "")
        {
            //Gets the IP Address if needed
            string strHost = Convert.ToString(Request.ServerVariables["LOCAL_ADDR"]);

            //Gets the domain name in www.*****.org format
            string strHost2 = Convert.ToString(Request.Url.Host);

            if (strHost2 == "www.wdpa-marine.org")
            {
                Session["Marine"] = "Active";
            }

        }

        else if (strMarine == "Active")
        {
            divGenMarine.Visible = true;

            if (!Page.IsPostBack)
            {
                //RBMPATerrestrial.Visible = false;
                //RBMPAAll.Visible = false;
                //RBMPAAll.Enabled = false;
                //RBMPACoastal.Visible = true;
                //RBMPAAllMarine.Visible = true;
                //RBMPAAllMarine.Checked = true;

                //divWdpaMarineTitleHolder.Visible = true;
                //divWdpaTitleHolder.Visible = false;

                RBMPAMarine.Text = "Marine Only";
                RBMPATerrestrialCoastal.Text = "Marine & Terrestrial";

                WDPAHelpAll.Visible = false;
                WDPAMarineHelpAll.Visible = true;

                WDPAHelpMarine.Visible = false;
                WDPAMarineHelpMarine.Visible = true;

                WDPAHelpTerrestrial.Visible = false;
                WDPAMarineHelpTerrestrial.Visible = true;

                //Country//
                SqlDataSource4.SelectCommand = "SELECT DISTINCT Country.ID, Country.ShortName " +
                    "FROM Country INNER JOIN " + strPolygonDB + " ON Country.ID = " + strPolygonDB + ".Country " +
                    "WHERE (Country.ID in ( 15, 33, 32, 48, 59, 192, 92, 108, 166, 116, 197, 221, 9, 29, 39, 51, 61, 87, 97, 136, 176, 123, 212, 226, 26, 31, 42,52, 63, 89, 95, 157, 168, 225, 222)) " +
                    "UNION " +
                    "SELECT DISTINCT Country_1.ID, Country_1.ShortName " +
                    "FROM Country AS Country_1 INNER JOIN " + strPointDB + " ON Country_1.ID = " + strPointDB + ".Country " +
                    "WHERE (Country_1.ID in ( 15, 33, 32, 48, 59, 192, 92, 108, 166, 116, 197, 221, 9, 29, 39, 51, 61, 87, 97, 136, 176, 123, 212, 226, 26, 31, 42,52, 63, 89, 95, 157, 168, 225, 222)) " +
                    "ORDER BY Country.ShortName";

                //Designation//
                SqlDataSource2.SelectCommand = "SELECT DISTINCT DesignationNew.ID, DesignationNew.WCMCDescription " +
                    "FROM DesignationNew INNER JOIN " + strPolygonDB + " ON DesignationNew.ID = " + strPolygonDB + ".Local_Designation " +
                    "WHERE (DesignationNew.WCMCDescription <> 'Not Applicable') " +
                    "UNION " +
                    "SELECT DISTINCT Designation_1.ID, Designation_1.WCMCDescription " +
                    "FROM DesignationNew AS Designation_1 INNER JOIN " + strPointDB + " ON Designation_1.ID = " + strPointDB + ".Local_Designation " +
                    "WHERE (Designation_1.WCMCDescription <> 'Not Applicable') " +
                    "ORDER BY WCMCDescription";

                //IUCNCategory//
                SqlDataSource1.SelectCommand = "SELECT DISTINCT IUCNCategory, CASE WHEN IUCNCategory = 'Not known' THEN 2 ELSE 1 END AS OrderSequence " +
                    "FROM " + strPolygonDB + " " +
                    "WHERE (IUCNCategory <> 'Not Applicable') AND (IUCNCategory <> '-1') AND (IUCNCategory <> '-999') " +
                    "UNION " +
                    "SELECT DISTINCT IUCNCategory, CASE WHEN IUCNCategory = 'Not known' THEN 2 ELSE 1 END AS OrderSequence " +
                    "FROM " + strPointDB + " " +
                    "WHERE (IUCNCategory <> 'Not Applicable') AND (IUCNCategory <> '-1') AND (IUCNCategory <> '-999') " +
                    "ORDER BY OrderSequence, IUCNCategory";

                //Convention//
                SqlDataSource3.SelectCommand = "SELECT DISTINCT IRConventionEntity.ID, IRConventionEntity.NAME " +
                    "FROM IRConventionEntity INNER JOIN " + strPolygonDB + " ON IRConventionEntity.ID = " + strPolygonDB + ".International_Convention " +
                    "WHERE IRConventionEntity.NAME <> 'Not Applicable' " +
                    "UNION " +
                    "SELECT DISTINCT IRConventionEntity_1.ID, IRConventionEntity_1.NAME " +
                    "FROM IRConventionEntity AS IRConventionEntity_1 INNER JOIN " + strPointDB + " ON IRConventionEntity_1.ID = " + strPointDB + ".International_Convention " +
                    "WHERE IRConventionEntity_1.NAME <> 'Not Applicable' " +
                    "ORDER BY IRConventionEntity.NAME";

                //Criteria//
                //SqlDataSource5.SelectCommand = "SELECT DISTINCT Criteria.ID, Criteria.Description " +
                //    "FROM Criteria INNER JOIN " + strPolygonDB + " ON Criteria.ID = " + strPolygonDB + ".Criteria " +
                //    "WHERE (" + strPolygonDB + ".MarineTerrestrial IN ('Marine', 'Both')) AND Criteria.Description <> 'Not Applicable' " +
                //    "UNION " +
                //    "SELECT DISTINCT Criteria_1.ID, Criteria_1.Description " +
                //    "FROM Criteria AS Criteria_1 INNER JOIN " + strPointDB + " ON Criteria_1.ID = " + strPointDB + ".Criteria " +
                //    "WHERE (" + strPointDB + ".MarineTerrestrial IN ('Marine', 'Both')) AND Criteria_1.Description <> 'Not Applicable' " +
                //    "ORDER BY Criteria.Description";
            }
        }

        if (!Page.IsPostBack)
        {
            string strDEFDesSQL = Convert.ToString(SqlDataSource2.SelectCommand);
            string strDEFIUCNCatSQL = Convert.ToString(SqlDataSource1.SelectCommand);
            string strDEFConventionSQL = Convert.ToString(SqlDataSource3.SelectCommand);
            //string strDEFCriteriaSQL = Convert.ToString(SqlDataSource5.SelectCommand);

            Session["DEFDesSQL"] = strDEFDesSQL;
            Session["DEFIUCNCatSQL"] = strDEFIUCNCatSQL;
            Session["DEFConventionSQL"] = strDEFConventionSQL;
            //Session["DEFCriteriaSQL"] = strDEFCriteriaSQL;
        }


        string strSearchType = Convert.ToString(Session["SearchType"]);

        if (strSearchType == "")
        {
            Session["SearchType"] = "National";
            FSNational.Visible = true;
            FSInternational.Visible = false;
            //RBAll.Checked = true;
            AllSpacer.Visible = false;

        }

        else if (strSearchType == "International")
        {
            RBNatSearch.Checked = false;
            //RBAll.Checked = false;
            RBIntSearch.Checked = true;

            FSNational.Visible = false;
            FSInternational.Visible = true;

            AllSpacer.Visible = false;
        }

        else if (strSearchType == "National")
        {
            RBIntSearch.Checked = false;
            //RBAll.Checked = false;
            RBNatSearch.Checked = true;

            FSNational.Visible = true;
            FSInternational.Visible = false;

            AllSpacer.Visible = false;
        }
        else
        {
            Session["SearchType"] = "National";
            FSNational.Visible = true;
            FSInternational.Visible = false;
            //RBAll.Checked = true;
            AllSpacer.Visible = false;
        }


        //ScriptManager.RegisterStartupScript(Page, this.GetType(), "Reposition", "document.getElementById('" + Convention.ClientID + "').selectedIndex=" + Convention.SelectedIndex + "; ", true);
    }

    protected void Country_SelectedIndexChanged(object sender, EventArgs e)
    {
        //string dbandUser = CodeConfig.DatabaseAndUser;
        string strPolygonDB = CodeConfig.PolygonDB;
        string strPointDB = CodeConfig.PointDB;
        string strMarine = Convert.ToString(Session["Marine"]);

        if (Country.SelectedItem.Value != "")
        {

            string strWhereOne;
            string strWhereTwo;

            //Set Where Clauses based on Marine Session Variable//
            if (strMarine == "Active")
            {
                strWhereOne = "WHERE (" + strPolygonDB + ".Country = '" + Country.SelectedItem.Value + "') " +
                 "AND (" + strPolygonDB + ".MarineTerrestrial IN ('Marine', 'Both')) ";

                strWhereTwo = "WHERE (" + strPointDB + ".Country = '" + Country.SelectedItem.Value + "') " +
                    "AND (" + strPointDB + ".MarineTerrestrial IN ('Marine', 'Both')) ";
            }

            else
            {
                strWhereOne = "WHERE (" + strPolygonDB + ".Country = '" + Country.SelectedItem.Value + "') ";

                strWhereTwo = "WHERE (" + strPointDB + ".Country = '" + Country.SelectedItem.Value + "') ";
            }

            //Designation//
            StringBuilder strBuildDesignation = new StringBuilder();

            string strDesOne = "SELECT DISTINCT DesignationNew.ID, DesignationNew.WCMCDescription " +
                "FROM DesignationNew INNER JOIN " + strPolygonDB + " ON DesignationNew.ID = " + strPolygonDB + ".English_Designation " +
                "INNER JOIN Country ON " + strPolygonDB + ".Country = Country.ID ";

            string strDesWhereOne = "AND (DesignationNew.WCMCDescription <> 'Not Applicable') ";

            string strDesTwo = "UNION SELECT DISTINCT Designation_1.ID, Designation_1.WCMCDescription " +
                "FROM DesignationNew AS Designation_1 INNER JOIN " + strPointDB + " ON Designation_1.ID = " + strPointDB + ".English_Designation " +
                "INNER JOIN Country AS Country_1 ON " + strPointDB + ".Country = Country_1.ID ";

            string strDesWhereTwo = "AND (Designation_1.WCMCDescription <> 'Not Applicable') ";

            string strDesThree = "ORDER BY DesignationNew.WCMCDescription";

            strBuildDesignation.Append(strDesOne + strWhereOne + strDesWhereOne + strDesTwo + strWhereTwo + strDesWhereTwo + strDesThree);

            string DesSQL = Convert.ToString(strBuildDesignation);

            SqlDataSource2.SelectCommand = DesSQL;

            Designation.Items.Clear();
            ListItem DesignationItem = new ListItem("All", "");
            Designation.Items.Add(DesignationItem);
            DesignationItem.Selected = true;
            Designation.DataSourceID = "SqlDataSource2";
            Designation.AppendDataBoundItems = true;

            //IUCNCategory
            StringBuilder strBuildIUCNCat = new StringBuilder();

            string strIUCNCatOne = "SELECT DISTINCT IUCNCategory, CASE WHEN IUCNCategory = 'Not known' THEN 2 ELSE 1 END AS OrderSequence " +
                "FROM " + strPolygonDB + " " +
                "INNER JOIN Country ON " + strPolygonDB + ".Country = Country.ID ";

            string strIUCNCatTwo = "UNION SELECT DISTINCT IUCNCategory, CASE WHEN IUCNCategory = 'Not known' THEN 2 ELSE 1 END AS OrderSequence " +
                "FROM " + strPointDB + " " +
                "INNER JOIN Country AS Country_1 ON " + strPointDB + ".Country = Country_1.ID ";

            string strIUCNCatThree = "AND (IUCNCategory <> 'Not Applicable') AND (IUCNCategory <> '-1') AND (IUCNCategory <> '-999') ";

            string strIUCNCatFour = "ORDER BY OrderSequence, IUCNCategory";

            strBuildIUCNCat.Append(strIUCNCatOne + strWhereOne + strIUCNCatThree + strIUCNCatTwo + strWhereTwo + strIUCNCatThree + strIUCNCatFour);

            string IUCNCatSQL = Convert.ToString(strBuildIUCNCat);

            SqlDataSource1.SelectCommand = IUCNCatSQL;

            IUCNCategory.Items.Clear();
            ListItem IUCNCatItem = new ListItem("All", "");
            IUCNCategory.Items.Add(IUCNCatItem);
            IUCNCatItem.Selected = true;
            IUCNCategory.DataSourceID = "SqlDataSource1";
            IUCNCategory.AppendDataBoundItems = true;

            //Convention
            StringBuilder strBuildConvention = new StringBuilder();

            string strConventionOne = "SELECT DISTINCT IRConventionEntity.ID, IRConventionEntity.NAME " +
                "FROM IRConventionEntity INNER JOIN " + strPolygonDB + " ON IRConventionEntity.ID = " + strPolygonDB + ".International_Convention " +
                "INNER JOIN Country ON " + strPolygonDB + ".Country = Country.ID ";

            string strConventionTwo = "UNION SELECT DISTINCT IRConventionEntity_1.ID, IRConventionEntity_1.NAME " +
                "FROM IRConventionEntity AS IRConventionEntity_1 INNER JOIN " + strPointDB + " ON IRConventionEntity_1.ID = " + strPointDB + ".International_Convention " +
                "INNER JOIN Country AS Country_1 ON " + strPointDB + ".Country = Country_1.ID ";

            string strConventionThree = "AND (NAME <> 'Not Applicable') ";

            string strConventionFour = "ORDER BY IRConventionEntity.NAME";

            strBuildConvention.Append(strConventionOne + strWhereOne + strConventionThree + strConventionTwo + strWhereTwo + strConventionThree + strConventionFour);

            string ConventionSQL = Convert.ToString(strBuildConvention);

            SqlDataSource3.SelectCommand = ConventionSQL;

            Convention.Items.Clear();
            ListItem ConventionItem = new ListItem("All", "");
            Convention.Items.Add(ConventionItem);
            ConventionItem.Selected = true;
            Convention.DataSourceID = "SqlDataSource3";
            Convention.AppendDataBoundItems = true;

            //Criteria
            //StringBuilder strBuildCriteria = new StringBuilder();

            //string strCriteriaOne = "SELECT DISTINCT Criteria.ID, Criteria.Description " +
            //    "FROM Criteria INNER JOIN " + strPolygonDB + " ON Criteria.ID = " + strPolygonDB + ".Criteria " +
            //    "INNER JOIN Country ON " + strPolygonDB + ".Country = Country.ID ";

            //string strCriteriaTwo = "UNION SELECT DISTINCT Criteria_1.ID, Criteria_1.Description " +
            //    "FROM Criteria AS Criteria_1 INNER JOIN " + strPointDB + " ON Criteria_1.ID = " + strPointDB + ".Criteria " +
            //    "INNER JOIN Country AS Country_1 ON " + strPointDB + ".Country = Country_1.ID ";

            //string strCriteriaThree = "AND (Description <> 'Not Applicable') ";

            //string strCriteriaFour = "ORDER BY Criteria.Description";

            //strBuildCriteria.Append(strCriteriaOne + strWhereOne + strCriteriaThree + strCriteriaTwo + strWhereTwo + strCriteriaThree + strCriteriaFour);

            //string CriteriaSQL = Convert.ToString(strBuildCriteria);

            //SqlDataSource5.SelectCommand = CriteriaSQL;

            //Criteria.Items.Clear();
            //ListItem CriteriaItem = new ListItem("All", "");
            //Criteria.Items.Add(CriteriaItem);
            //CriteriaItem.Selected = true;
            //Criteria.DataSourceID = "SqlDataSource5";
            //Criteria.AppendDataBoundItems = true;

        }
        else
        {

            //Get the Default Select Commands
            string strDEFDesSQL = Convert.ToString(Session["DEFDesSQL"]);
            string strDEFIUCNCatSQL = Convert.ToString(Session["DEFIUCNCatSQL"]);
            string strDEFConventionSQL = Convert.ToString(Session["DEFConventionSQL"]);
            //string strDEFCriteriaSQL = Convert.ToString(Session["DEFCriteriaSQL"]);

            //Set the SQlDataSources.SelectCommand back to the default
            SqlDataSource2.SelectCommand = strDEFDesSQL;
            SqlDataSource1.SelectCommand = strDEFIUCNCatSQL;
            SqlDataSource3.SelectCommand = strDEFConventionSQL;
            //SqlDataSource5.SelectCommand = strDEFCriteriaSQL;

            //Designation Reset//
            Designation.Items.Clear();
            ListItem DesignationItem = new ListItem("All", "");
            Designation.Items.Add(DesignationItem);
            Designation.DataSourceID = "SqlDataSource2";
            DesignationItem.Selected = true;
            Designation.AppendDataBoundItems = true;

            //IUCN Category Reset//
            IUCNCategory.Items.Clear();
            ListItem IUCNCategoryItem = new ListItem("All", "");
            IUCNCategory.Items.Add(IUCNCategoryItem);
            IUCNCategory.DataSourceID = "SqlDataSource1";
            IUCNCategoryItem.Selected = true;
            IUCNCategory.AppendDataBoundItems = true;

            //Convention Reset//
            Convention.Items.Clear();
            ListItem ConventionItem = new ListItem("All", "");
            Convention.Items.Add(ConventionItem);
            Convention.DataSourceID = "SqlDataSource3";
            ConventionItem.Selected = true;
            Convention.AppendDataBoundItems = true;

            //Criteria Reset//
            //Criteria.Items.Clear();
            //ListItem CriteriaItem = new ListItem("All", "");
            //Criteria.Items.Add(CriteriaItem);
            //Criteria.DataSourceID = "SqlDataSource5";
            //CriteriaItem.Selected = true;
            //Criteria.AppendDataBoundItems = true;

        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {

        string strCountryQry = "";
        string strMPAQry = "";

        string strDesignationQry = "";
        string strIUCNCategoryQry = "";
        string strConventionQry = "";

        string strDesignationptQry = "";
        string strIUCNCategoryptQry = "";
        string strConventionptQry = "";


        string strCountryID = Convert.ToString(Country.SelectedItem.Value);
        //string strCriteriaQry = "";


        string strDTMA1 = "";
        string strDTMA2 = "";
        string strMarineComponentSub = "";
        string strMarineComponentInt = "";
        string strNoTakeReg = "";

        //string strGISTotalAreapt1 = "";
        //string strGISTotalAreapt2 = "";
        string strMarineComponentSubpt = "";
        string strMarineComponentIntpt = "";
        string strNoTakeRegpt = "";

        //string dbandUser = CodeConfig.DatabaseAndUser;

        string strPolygonDB = CodeConfig.PolygonDB;
        string strPointDB = CodeConfig.PointDB;


        if (Country.SelectedItem.Value != "")
        {

            strCountryQry = " IN ('" + Country.SelectedItem.Value + "')) ";

        }

        //original that includes UK

        //else
        //{

        //    strCountryQry = " LIKE '%') ";

        //}


        //New one that excludes UK
        else
        {
            strCountryQry = " <> '77') ";
        }



        //START OF MULTI MPA SELECT QUERY

        //if (MPA.SelectedItem.Value == "")
        //{
        //    strMPAQry = " LIKE '%')  ";
        //}
        //else
        //{
        //    strMPAQry = " IN (" + tbMPA.Text + ")) "; 
        //}

        //END OF MULTI MPA SELECT QUERY

        //START OF NEW MULTI MPA SELECT QUERY


        string strMarine = Convert.ToString(Session["Marine"]);

        if (RBMPAAll.Checked == true)
        {
            if (strMarine == "Active")
            {
                strMPAQry = " IN ('Both', 'Marine')) ";
            }
            else
            {
                strMPAQry = " IN ('Both', 'Marine', 'Terrestrial')) ";
            }
        }

        else if (RBMPATerrestrialCoastal.Checked == true)
        {
            if (strMarine == "Active")
            {
                strMPAQry = " IN ('Both')) ";
            }
            else
            {
                strMPAQry = " IN ('Both', 'Terrestrial')) ";
            }
        }

        else if (RBMPAMarine.Checked == true)
        {
            if (strMarine == "Active")
            {
                strMPAQry = " IN ('Marine')) ";
            }
            else
            {
                strMPAQry = " IN ('Both', 'Marine')) ";
            }

        }

        //END OF NEW MULTI MPA SELECT QUERY


        //START OF MULTI DESIGNATION SELECT QUERY

        if (Designation != null)
        {
            if (Designation.SelectedItem.Value == "")
            {
                strDesignationQry = " LIKE '%' OR " + strPolygonDB + ".English_Designation IS NULL)  ";
                strDesignationptQry = " LIKE '%' OR " + strPointDB + ".English_Designation IS NULL)  ";
            }
            else
            {
                strDesignationQry = " IN (" + tbDesignation.Text + ")) ";
                strDesignationptQry = " IN (" + tbDesignation.Text + ")) ";
            }
        }
        else
        {
            strDesignationQry = " LIKE '%' OR " + strPolygonDB + ".English_Designation IS NULL)  ";
            strDesignationptQry = " LIKE '%' OR " + strPointDB + ".English_Designation IS NULL)  ";
        }

        //END OF MULTI DESIGNATION SELECT QUERY

        //START OF MULTI IUCNCAT SELECT QUERY

        if (IUCNCategory != null)
        {

            if (IUCNCategory.SelectedItem.Value == "")
            {
                strIUCNCategoryQry = " LIKE '%' OR " + strPolygonDB + ".IUCNCategory IS NULL)  ";
                strIUCNCategoryptQry = " LIKE '%' OR " + strPointDB + ".IUCNCategory IS NULL)  ";
            }
            else
            {
                strIUCNCategoryQry = " IN (" + tbIUCNCategory.Text + ")) ";
                strIUCNCategoryptQry = " IN (" + tbIUCNCategory.Text + ")) ";
            }
        }
        else
        {
            strIUCNCategoryQry = " LIKE '%' OR " + strPolygonDB + ".IUCNCategory IS NULL)  ";
            strIUCNCategoryptQry = " LIKE '%' OR " + strPointDB + ".IUCNCategory IS NULL)  ";
        }

        //END OF MULTI IUCNCAT SELECT QUERY

        //START OF MULTI CONVENTION SELECT QUERY

        if (Convention != null)
        {
            if (Convention.SelectedItem.Value == "")
            {
                strConventionQry = " LIKE '%' OR " + strPolygonDB + ".International_Convention IS NULL)  ";
                strConventionptQry = " LIKE '%' OR " + strPointDB + ".International_Convention IS NULL)  ";
            }
            else
            {
                strConventionQry = " IN (" + tbConvention.Text + ")) ";
                strConventionptQry = " IN (" + tbConvention.Text + ")) ";
            }
        }
        else
        {
            strConventionQry = " LIKE '%' OR " + strPolygonDB + ".International_Convention IS NULL)  ";
            strConventionptQry = " LIKE '%' OR " + strPointDB + ".International_Convention IS NULL)  ";
        }

        //END OF MULTI CONVENTION SELECT QUERY

        //START OF MULTI CRITERIA SELECT QUERY

        //if (Criteria.SelectedItem.Value == "")
        //{
        //    strCriteriaQry = " LIKE '%')  ";
        //}
        //else
        //{
        //    strCriteriaQry = " IN (" + tbCriteria.Text + ")) ";
        //}

        //END OF MULTI CRITERIA SELECT QUERY

        //START OF DOCUMENTEDTOTALMARINEAREA SELECT QUERY

        string strRange1 = "";
        string strRange2 = "";

        if (DDLDocTotalMarineArea.SelectedItem.Value == "0")
        {
            strRange1 = "1000";
        }
        else if (DDLDocTotalMarineArea.SelectedItem.Value == "1")
        {
            strRange1 = "1000";
            strRange2 = "5000";
        }
        else if (DDLDocTotalMarineArea.SelectedItem.Value == "2")
        {
            strRange1 = "5000";
            strRange2 = "10000";
        }
        else if (DDLDocTotalMarineArea.SelectedItem.Value == "3")
        {
            strRange1 = "10000";
            strRange2 = "25000";
        }
        else if (DDLDocTotalMarineArea.SelectedItem.Value == "4")
        {
            strRange1 = "25000";
            strRange2 = "50000";
        }
        else if (DDLDocTotalMarineArea.SelectedItem.Value == "5")
        {
            strRange1 = "50000";
            strRange2 = "100000";
        }
        else if (DDLDocTotalMarineArea.SelectedItem.Value == "6")
        {
            strRange1 = "100000";
            strRange2 = "500000";
        }
        else if (DDLDocTotalMarineArea.SelectedItem.Value == "7")
        {
            strRange1 = "500000";
            strRange2 = "1000000";
        }
        else if (DDLDocTotalMarineArea.SelectedItem.Value == "8")
        {
            strRange1 = "1000000";
        }

        if (DDLDocTotalMarineArea.SelectedItem.Value == "")
        {
            strDTMA1 = " LIKE '%' OR " + strPolygonDB + ".DocumentedTotalMarineArea IS NULL)) ";
            strDTMA2 = " LIKE '%' OR " + strPointDB + ".DocumentedTotalMarineArea IS NULL)) ";
        }
        else if (DDLDocTotalMarineArea.SelectedItem.Value == "0")
        {
            strDTMA1 = " <= " + strRange1 + ") OR (" + strPolygonDB + ".DocumentedTotalMarineArea IS NULL)) ";
            strDTMA2 = " <= " + strRange1 + ") OR (" + strPointDB + ".DocumentedTotalMarineArea IS NULL)) ";
        }
        else if (DDLDocTotalMarineArea.SelectedItem.Value == "8")
        {
            strDTMA1 = " >= " + strRange1 + ")) ";
            strDTMA2 = " >= " + strRange1 + ")) ";
        }
        else
        {
            strDTMA1 = " >= " + strRange1 + ") AND (" + strPolygonDB + ".DocumentedTotalMarineArea <= " + strRange2 + ")) ";
            strDTMA2 = " >= " + strRange1 + ") AND (" + strPointDB + ".DocumentedTotalMarineArea <= " + strRange2 + ")) ";
        }


        //END OF DOCUMENTEDTOTALMARINEAREA SELECT QUERY

        //START OF MARINECOMPONENT QUERY

        if (CBSubtidal.Checked == true)
        {
            strMarineComponentSub = " = 'Yes') ";
            strMarineComponentSubpt = " = 'Yes') ";
        }
        else
        {
            strMarineComponentSub = " LIKE '%' OR " + strPolygonDB + ".Subtidal IS NULL) ";
            strMarineComponentSubpt = " LIKE '%' OR " + strPointDB + ".Subtidal IS NULL) ";
        }

        if (CBIntertidal.Checked == true)
        {
            strMarineComponentInt = " = 'Yes') ";
            strMarineComponentIntpt = " = 'Yes') ";
        }
        else
        {
            strMarineComponentInt = " LIKE '%' OR " + strPolygonDB + ".Intertidal IS NULL) ";
            strMarineComponentIntpt = " LIKE '%' OR " + strPointDB + ".Intertidal IS NULL) ";
        }

        if (DDLNoTakeReg.SelectedItem.Value == "")
        {
            strNoTakeReg = " LIKE '%' OR " + strPolygonDB + ".NoTakeProportion IS NULL) ";
            strNoTakeRegpt = " LIKE '%' OR " + strPointDB + ".NoTakeProportion IS NULL) ";
        }
        else
        {
            strNoTakeReg = " = '" + DDLNoTakeReg.SelectedItem.Value + "') ";
            strNoTakeRegpt = " = '" + DDLNoTakeReg.SelectedItem.Value + "') ";
        }




        //END OF MARINECOMPONENT QUERY

        Session["GenCountryID"] = strCountryQry;
        Session["GenMPAID"] = strMPAQry;

        Session["NatDesignationID"] = strDesignationQry;
        Session["NatDesignationptID"] = strDesignationptQry;
        Session["NatIUCNCatID"] = strIUCNCategoryQry;
        Session["NatIUCNCatptID"] = strIUCNCategoryptQry;
        Session["IntConventionID"] = strConventionQry;
        Session["IntConventionptID"] = strConventionptQry;
        //Session["IntCriteriaID"] = strCriteriaQry;
        Session["GenDTMA1"] = strDTMA1;
        Session["GenDTMA2"] = strDTMA2;
        Session["GenMarineComponentSub"] = strMarineComponentSub;
        Session["GenMarineComponentSubpt"] = strMarineComponentSubpt;
        Session["GenMarineComponentInt"] = strMarineComponentInt;
        Session["GenMarineComponentIntpt"] = strMarineComponentIntpt;
        Session["GenNoTakeReg"] = strNoTakeReg;
        Session["GenNoTakeRegpt"] = strNoTakeRegpt;

        //System.Threading.Thread.Sleep(5000);

        Response.Redirect("MultiResult.aspx?Country=" + strCountryID);

    }

    protected void RBNatSearch_CheckedChanged(object sender, EventArgs e)
    {
        RBIntSearch.Checked = false;
        //RBAll.Checked = false;
        RBNatSearch.Checked = true;

        Session["SearchType"] = "National";

        FSNational.Visible = true;
        FSInternational.Visible = false;

        AllSpacer.Visible = false;

        //System.Threading.Thread.Sleep(5000);
    }
    protected void RBIntSearch_CheckedChanged(object sender, EventArgs e)
    {

        RBNatSearch.Checked = false;
        //RBAll.Checked = false;
        RBIntSearch.Checked = true;

        Session["SearchType"] = "International";

        FSNational.Visible = false;
        FSInternational.Visible = true;

        AllSpacer.Visible = false;

        //System.Threading.Thread.Sleep(5000);

    }

    //protected void RBAll_CheckedChanged(object sender, EventArgs e)
    //{
    //    RBNatSearch.Checked = false;
    //    RBIntSearch.Checked = false;
    //    RBAll.Checked = true;

    //    Session["SearchType"] = "All";

    //    FSNational.Visible = true;
    //    FSInternational.Visible = true;

    //    AllSpacer.Visible = true;

    //    //System.Threading.Thread.Sleep(5000);

    //}

    protected void Convention_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strMarine = Convert.ToString(Session["Marine"]);
        string strDEFCriteriaSQL = Convert.ToString(Session["DEFCriteriaSQL"]);
        string dbandUser = CodeConfig.DatabaseAndUser;


        if (Convention.SelectedIndex == -1)
        {
            //This code is hit
            Convention.SelectedIndex = 0;
        }
        else
        {
            if (strMarine == "Active")
            {
                if (Convention.SelectedItem.Value != "")
                {
                    //Criteria.Items.Clear();
                    //ListItem CriteriaItem = new ListItem("All", "");
                    //Criteria.Items.Add(CriteriaItem);
                    //SqlDataSource5.SelectCommand = "SELECT DISTINCT Criteria.ID, Criteria.Description " +
                    //    "FROM Criteria INNER JOIN " +
                    //    dbandUser + ".CurrentSiteWDPA ON Criteria.ID = " + strPolygonDB + ".Criteria " +
                    //    "WHERE " + strPolygonDB + ".International_Convention IN (" + tbConvention.Text + ") AND (" + strPolygonDB + ".MarineTerrestrial IN ('Marine', 'Both')) AND (Criteria.Description <> 'Not Applicable') " +
                    //    "UNION " +
                    //    "SELECT DISTINCT Criteria_1.ID, Criteria_1.Description " +
                    //    "FROM Criteria AS Criteria_1 INNER JOIN " +
                    //    dbandUser + ".CurrentSiteWDPApt ON Criteria_1.ID = " + strPointDB + ".Criteria " +
                    //    "WHERE " + strPointDB + ".International_Convention IN (" + tbConvention.Text + ") AND (" + strPointDB + ".MarineTerrestrial IN ('Marine', 'Both')) AND (Criteria_1.Description <> 'Not Applicable') " +
                    //    "ORDER BY Criteria.Description";
                    //Criteria.DataSourceID = "SqlDataSource5";
                    //CriteriaItem.Selected = true;
                    //Criteria.AppendDataBoundItems = true;
                }
                else
                {
                    //Criteria.Items.Clear();
                    //ListItem CriteriaItem = new ListItem("All", "");
                    //Criteria.Items.Add(CriteriaItem);
                    //SqlDataSource5.SelectCommand = strDEFCriteriaSQL;
                    //Criteria.DataSourceID = "SqlDataSource5";
                    //CriteriaItem.Selected = true;
                    //Criteria.AppendDataBoundItems = true;
                }

            }
            else
            {
                if (Convention.SelectedItem.Value != "")
                {
                    //Criteria.Items.Clear();
                    //ListItem CriteriaItem = new ListItem("All", "");
                    //Criteria.Items.Add(CriteriaItem);
                    //SqlDataSource5.SelectCommand = "SELECT DISTINCT Criteria.ID, Criteria.Description " +
                    //    "FROM Criteria INNER JOIN " +
                    //    dbandUser + ".CurrentSiteWDPA ON Criteria.ID = " + strPolygonDB + ".Criteria " +
                    //    "WHERE " + strPolygonDB + ".International_Convention IN (" + tbConvention.Text + ") AND (Criteria.Description <> 'Not Applicable') " +
                    //    "UNION " +
                    //    "SELECT DISTINCT Criteria_1.ID, Criteria_1.Description " +
                    //    "FROM Criteria AS Criteria_1 INNER JOIN " +
                    //    dbandUser + ".CurrentSiteWDPApt ON Criteria_1.ID = " + strPointDB + ".Criteria " +
                    //    "WHERE " + strPointDB + ".International_Convention IN (" + tbConvention.Text + ") AND (Criteria_1.Description <> 'Not Applicable') " +
                    //    "ORDER BY Criteria.Description";
                    //Criteria.DataSourceID = "SqlDataSource5";
                    //CriteriaItem.Selected = true;
                    //Criteria.AppendDataBoundItems = true;
                }
                else
                {
                    //Criteria.Items.Clear();
                    //ListItem CriteriaItem = new ListItem("All", "");
                    //Criteria.Items.Add(CriteriaItem);
                    //SqlDataSource5.SelectCommand = strDEFCriteriaSQL;
                    //Criteria.DataSourceID = "SqlDataSource5";
                    //CriteriaItem.Selected = true;
                    //Criteria.AppendDataBoundItems = true;
                }
            }
        }
    }

    protected void RBMPASelect_CheckedChanged(object sender, EventArgs e)
    {
        if (RBMPAAll.Checked == true)
        {
            RBMPAMarine.Checked = false;
            RBMPATerrestrialCoastal.Checked = false;
            RBMPAAll.Checked = true;
        }

        else if (RBMPAMarine.Checked == true)
        {
            RBMPAAll.Checked = false;
            RBMPATerrestrialCoastal.Checked = false;
            RBMPAMarine.Checked = true;
        }
        else if (RBMPATerrestrialCoastal.Checked == true)
        {
            RBMPAAll.Checked = false;
            RBMPAMarine.Checked = false;
            RBMPATerrestrialCoastal.Checked = true;
        }
    }

    ////public override void VerifyRenderingInServerForm(Control control)
    ////{
    ////    base.VerifyRenderingInServerForm(control);
    ////}
}
